﻿using System;

namespace YinLongTool.Number
{
    public static class DecimalUtil
    {
        /// <summary>
        /// 字符串转decimal
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns>int类型的数字</returns>
        public static decimal ToDecimal(string s)
        {
            Decimal.TryParse(s, out var result);
            return result;
        }


        /// <summary>
        /// 字符串转decimal
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns>int类型的数字</returns>
        public static decimal ToDecimal(string s, int defaultValue = 0)
        {
            return decimal.TryParse(s, out var result) ? result : defaultValue;
        }

        /// <summary>
        /// 将int转换成decimal
        /// </summary>
        /// <param name="num">int类型</param>
        /// <returns>int类型</returns>
        public static decimal ToDecimal(int num)
        {
            return new decimal(num);
        }
        /// <summary>
        /// 字符串转decimal
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns>int类型的数字</returns>
        public static decimal ToDecimal(double s)
        {
            return new decimal(s);
        }
    }
}